//require CodeRAD
package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.rad.models.Entity;
import com.codename1.rad.models.EntityList;
import static com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder;
import com.codename1.rad.nodes.ListNode;
import com.codename1.rad.schemas.Thing;
import com.codename1.rad.ui.entityviews.EntityListView;
import com.codename1.rad.ui.entityviews.ProfileListView;
import com.codename1.ui.Button;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.GridLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class EntityListTransactionsSample {

    private Form current;
    private Resources theme;
    private EntityList entityList;
    private EntityListView entityListView;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    class RowEntity extends Entity {}
    {
        entityTypeBuilder(RowEntity.class)
                .factory(cls -> { return new RowEntity();})
                .string(Thing.name)
                .build();
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        entityList = new EntityList();
        entityListView = new ProfileListView(entityList);
        entityListView.setScrollableY(true);
        
        
        Button addTen = new Button("Add 10 rows");
        addTen.addActionListener(evt-> {
            addRows(10);
        });
        
        Button addHundred = new Button("Add 100 rows");
        addHundred.addActionListener(evt->{
            addRows(100);
        });
        
        Button removeTen = new Button("Remove 10 rows");
        removeTen.addActionListener(evt->{
            removeRows(10);
        });
        
        Form hi = new Form("Hi World", new BorderLayout());
        hi.add(NORTH, GridLayout.encloseIn(3, addTen, addHundred, removeTen));
        hi.add(CENTER, entityListView);
        hi.show();
    }
    
    private void addRows(int n) {
        entityList.startTransaction();
        for (int i=0; i<n; i++) {
            Entity e = new RowEntity();
            e.set(Thing.name, "Entity "+i);
            entityList.add(e);
        }
        entityList.commitTransaction();
    }
    
    
    private void removeRows(int n) {
        entityList.startTransaction();
        for (int i=0; i<n; i++) {
            if (entityList.size() > 0) {
                entityList.remove(entityList.get(0));
            }
        }
        entityList.commitTransaction();
    }
    

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
